<template>
  <div>
    <el-upload
      ref="upload"
      class="upload-demo"
      :headers="headers"
      :action="uploadUrl"
      :multiple="false"
      :data="temp"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :before-upload="uploadExl"
      :show-file-list='false'
      accept='.csv'
      :file-list="fileList">
      <slot name="up"></slot>
    </el-upload>
  </div>
</template>
<script>
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      fileList: [], // 初始的文件
      headers: {
        'X-Auth-Token': ''
      }, // 设置上传的请求头部
      uploadUrl: process.env.VUE_APP_BASE_API + '/api/aviation/insurance/upload', // 必选参数，上传的地址
      temp: {} // 上传时附带的额外参数
    }
  },
  methods: {
    // 设置
    uploadExl(file) {
      this.temp.formFile = file
      this.headers['X-Auth-Token'] = getToken()
    },
    // 上传成功
    uploadSuccess(response, file, fileList) {
      this.$emit('uploadSuccessP', { response, file, fileList })
    },
    // 上传失败
    uploadError(err, file, fileList) {
      this.$emit('uploadErrorP', { err, file, fileList })
    }
  }
}
</script>
